/**
 * HOMER Core - an open and flexible OSGi-based software platform for SmartHome and AAL applications
 *
 * Copyright (C) 2011-2013 AIT Austrian Institute of Technology GmbH - <http://www.ait.ac.at>
 * Contact: Thomas Fuxreiter <thomas.fuxreiter@ait.ac.at>
 *
 * This file is part of HOMER Core.
 *
 * HOMER Core is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * HOMER Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package at.aal2aac.eventbus.client.src.main.java.at.ac.ait.hbs.homer.core.eventbus.client;

import at.ac.ait.hbs.homer.core.common.homecontrol.HomeControlCommand;
import org.apache.log4j.Logger;

import at.ac.ait.hbs.homer.core.common.eventbus.EventbusConnector;
import at.ac.ait.hbs.homer.core.common.eventbus.EventbusMessage;
import at.ac.ait.hbs.homer.core.common.eventbus.EventbusMessageHandler;
import at.ac.ait.hbs.homer.core.common.homecontrol.enumerations.Command;


public class LocalEventbusClient {

	private static final Logger log = Logger.getLogger(LocalEventbusClient.class);

	private EventbusConnector eventbusConnector;
	private EventbusMessageHandler myHandler;
	private static final String ID = "LocalTestClient";

	/**
	 * Setter for the eventbus connector for OSGi dependency injection (see
	 * {@link blueprint.xml})
	 * 
	 * @param eventbusConnector
	 */

	public final void setEventbusConnector(final EventbusConnector eventbusConnector) {
		this.eventbusConnector = eventbusConnector;
	}

	public final void init() {


		myHandler = new StringEventbusMessageHandler();

		eventbusConnector.register(ID, "hello", myHandler);

		// publish a message;
		//eventbusConnector.publish(myMsg);

		//gui = new ClientDemoGui();
		//gui.init();
		//gui.addSendListener(this);
        Command c = Command.REQUEST_FLAT_CONFIG;
        HomeControlCommand hcm = new HomeControlCommand(c);
        onSend("homecontrol", hcm);
	}

	public final void destroy() {
		eventbusConnector.unregister(ID, "hello", myHandler);

	}

	private class StringEventbusMessageHandler implements EventbusMessageHandler {
		@Override
		public void handleEventbusMessage(final EventbusMessage msg) {
            System.out.println(msg);
            EventbusMessage m = msg;
            System.out.println(m);


		}
	}
       /*
    private class Message {
            Command cmd;
            int id=0;
            DeviceMessageType dmt=null;

           public Message(Command cmd, int id, DeviceMessageType dmt) {
                   this.cmd = cmd;
                   this.id = id;
                   this.dmt = dmt;
           }
    }   */


	public final void onSend(final String address, final HomeControlCommand message) {
		EventbusMessage<HomeControlCommand> myMsg = new EventbusMessage<HomeControlCommand>();

		myMsg.setAddress(address);
		myMsg.setBody(message);
		myMsg.setMode(EventbusMessage.PUBLISH);
        myMsg.setReplyAddress("hello");

		eventbusConnector.publish(myMsg);
	}
}
